package render.display
{
	import flash.display3D.Context3D;
	import flash.geom.Vector3D;

	public class RenderLayer
	{
		/**
		 *渲染对象列表 
		 */		
		private var _children:Vector.<RenderObject> = new Vector.<RenderObject>();
		/**
		 *Context3D 对象 
		 */		
		private var _context3D:Context3D;
		
		/**
		 *渲染层 
		 * 
		 */		
		public function RenderLayer(context3D:Context3D)
		{
			_context3D=context3D;
		}

		/**
		 *渲染 
		 * 
		 */		
		public function render():void
		{
			for each(var renderObject:RenderObject in _children)
			{
				renderObject.render();
			}
		}
		
		public function processMouseEvent(mouseEventType:String, mousePosition:Vector3D):void
		{
			for each(var renderObject:RenderObject in _children)
			{
				renderObject.processMouseEvent(mouseEventType, mousePosition);
			}
		}
		
		/**
		 *增加渲染对象 
		 * @param renderObject 渲染对象
		 * 
		 */		
		public function addChild(renderObject:RenderObject):void
		{
			renderObject.context3D=_context3D;
			_children.push(renderObject);
		}
		
		/**
		 *增加渲染对象至指定的索引位置 
		 * @param renderObject 渲染对象
		 * 
		 */		
		public function addChildAt(renderObject:RenderObject, index:int):void
		{
			_children.splice(index, 0, renderObject);
		}		
		
		/**
		 * 根据索引位置取得渲染对象
		 * @param index 索引位置 
		 * @return RenderObject
		 * 
		 */			
		public function getChildAt(index:int):RenderObject
		{
			if(_children.length<=index)return null;
			return _children[index];
		}
		
		/**
		 *根据渲染对象取得其索引位置 
		 * @param renderObject 渲染对象
		 * @return int
		 * 
		 */		
		public function getChildIndex(renderObject:RenderObject):int
		{
			return _children.indexOf(renderObject);
		}		
		
		/**
		 *移除存在的渲染对象，并且是否清理释放资源 
		 * @param renderObject 渲染对象
		 * @param isDispose 是否清理释放渲染对象资源
		 */		
		public function removeChild(renderObject:RenderObject, isDispose:Boolean=true):void
		{
			if(_children.indexOf(renderObject)!=-1)
			{
				_children.splice(_children.indexOf(renderObject), 1);
				if(isDispose)
				{
					renderObject.dispose();
					renderObject=null
				}
			}
		}
		
		/**
		 *移除存在的渲染对象，并且是否清理释放资源 
		 * @param index 渲染对象索引
		 * @param isDispose 是否清理释放渲染对象资源
		 */		
		public function removeChildAt(index:int, isDispose:Boolean=true):void
		{
			if(_children.length>index)
			{
				var renderObject:RenderObject=_children[index];
				if(isDispose)
				{
					renderObject.dispose();
					renderObject=null
				}
			}
		}
		
		/**
		 *指定的渲染对象是否存在 
		 * @param renderObject 渲染对象
		 * @return Boolean
		 * 
		 */
		public function contains(renderObject:RenderObject):Boolean
		{
			return _children.indexOf(renderObject)!=-1;
		}

		/**
		 *渲染对象列表 
		 */
		public function get children():Vector.<RenderObject>
		{
			return _children;
		}
		
		/**
		 *资源释放 
		 * 
		 */		
		public function dispose():void
		{
			while(_children && _children.length>0)
			{
				var renderObject:RenderObject=_children.shift();
				renderObject.dispose();
				renderObject=null;
			}
			_children=null;
			_context3D=null;
		}

		/**
		 * Context3D 对象
		 */
		public function get context3D():Context3D
		{
			return _context3D;
		}

		/**
		 * @private
		 */
		public function set context3D(value:Context3D):void
		{
			_context3D = value;
		}

	}
}